﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using TopDownShooter.Screens;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework;
using FlatRedBall.Math;
using FlatRedBall.Input;
using Microsoft.Xna.Framework.Input;

namespace TopDownShooter
{
    class Unit : PositionedObject
    {
        // Private variables
        Circle m_Circle;
        Line m_Line;
        GameScreen m_Screen;

        internal Weapon weapon;
        internal Shotgun shotgun;
        internal SMG smg;
        internal Pistol pistol;
        internal int Health = 100;
        internal HealthBar healthBar;

        internal Camera Camera;

        public Unit(GameScreen parent)
        {
            m_Circle = ShapeManager.AddCircle();
            m_Circle.AttachTo(this, false);
            m_Line = ShapeManager.AddLine();
            m_Line.SetFromAbsoluteEndpoints(new Vector3(.4f, 0, 0), Vector3.Zero);
            m_Line.AttachTo(this, false);
            m_Line.RelativeX = .5f;
            m_Screen = parent;
            shotgun = new Shotgun(this);
            smg = new SMG(this);
            pistol = new Pistol(this);
            weapon = pistol;
            healthBar = new HealthBar();
            healthBar.AttachTo(this, false);
            healthBar.RelativeY = 2;
        }
        public Circle Circle
        {
            get { return m_Circle; }
        }
        public GameScreen Screen
        {
            get { return m_Screen; }
        }
        public void PointAt(Vector3 point)
        {
            float angleToPointAt = 0;
            float difference = (point.Y - Y) / (point.X - X);
            angleToPointAt = (float)Math.Atan(difference);
            if (point.X < X)
                angleToPointAt += MathHelper.Pi;
            this.RotationZ = angleToPointAt;
        }
        public float speed = .5f;
        public PositionedObjectList<Bullet> bulletsFired = new PositionedObjectList<Bullet>();
        public void Activity()
        {
        }
        public bool ClearLineOfSightTo(PositionedObject obj)
        {
            Line lineOfSight = new Line();
            lineOfSight.SetFromAbsoluteEndpoints(Position, obj.Position);
            for (int i = 0; i < Screen.boxes.Count; i++)
            {
                if (Screen.boxes[i].Rectangle.CollideAgainst(lineOfSight))
                    return false;
            }
            return true;
        }
        protected virtual void SelectWeapon(Weapon w)
        {
            weapon = w;
            w.Selected();
        }

        protected virtual void Reload()
        {
            weapon.reloading = true;
        }
        public void Destroy()
        {
            healthBar.Destroy();
            ShapeManager.Remove(m_Circle);
            ShapeManager.Remove(m_Line);

            m_Circle.RemoveSelfFromListsBelongingTo();
            m_Line.RemoveSelfFromListsBelongingTo();
            RemoveSelfFromListsBelongingTo();
        }

        internal void AddHealth(int p)
        {
            if (Health + p > 100)
                Health = 100;
            else
                Health += p;
        }
    }
}
